//
//  TTScrollView.m
//  Tint
//
//  Created by Kevin Wu on 2/28/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTScrollView.h"



@implementation TTScrollView


#pragma mark - Accessors

@synthesize showShadows = _showShadows;

- (void)setShowShadows:(BOOL)showShadows {
    _showShadows = showShadows;
    [self setNeedsLayout];
}


#pragma mark - Memory

- (void)dealloc {
    TTRelease(_topShadowLayer);
    TTRelease(_bottomShadowLayer);
    [super dealloc];
}



#pragma mark - UIView

- (void)layoutSubviews {
	[super layoutSubviews];
	
    if (!_showShadows) {
        [_topShadowLayer removeFromSuperlayer];
        TTRelease(_topShadowLayer);
        
        [_bottomShadowLayer removeFromSuperlayer];
        TTRelease(_bottomShadowLayer);
        return ;
    }
    
    
    if (!_topShadowLayer) {
        CAGradientLayer *shadowLayer = [self shadowAsInverse:YES];
        TTRetain(shadowLayer, _topShadowLayer);
        [self.layer insertSublayer:_topShadowLayer atIndex:0];
    } else if ([self.layer.sublayers indexOfObjectIdenticalTo:_topShadowLayer] != 0) {
        [self.layer insertSublayer:_topShadowLayer atIndex:0];
    }
    _topShadowLayer.frame = TTRectMake(0.0, 0.0 - _topShadowLayer.frame.size.height, self.width, _topShadowLayer.frame.size.height);
    
    
    
    if (!_bottomShadowLayer) {
        CAGradientLayer *shadowLayer = [self shadowAsInverse:NO];
        TTRetain(shadowLayer, _bottomShadowLayer);
        [self.layer insertSublayer:_bottomShadowLayer atIndex:0];
    } else if ([self.layer.sublayers indexOfObjectIdenticalTo:_bottomShadowLayer] != 0) {
        [self.layer insertSublayer:_bottomShadowLayer atIndex:0];
    }
    _bottomShadowLayer.frame = TTRectMake(0.0, self.contentSize.height, self.width, _bottomShadowLayer.frame.size.height);
}

@end
